<template>
	<el-dialog v-model="dialogVisible" title="审核确认" width="60%" :lock-scroll="false">
		<div>
			<el-card v-if="topData">
				<el-form class="demo-form-inline">
					<el-row :gutter="20">
						<el-col :span="12">
							<el-form-item label="审批部门：">{{ topData.deptName }}</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="审批用户：">{{ topData.operationUser }}</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="审批结果：">
								<el-tag v-if="topData.approveResult == 'true'">通过</el-tag>
								<el-tag type="danger" v-else>不通过</el-tag>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="审批时间：">{{ topData.operationTime }}</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="审批意见：">{{ topData.approveDesc }}</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</el-card>
			<div style="margin-top: 20px">
				<HyForm :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef" />
			</div>
		</div>
		<template #footer>
			<div class="dialog-footer">
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" :loading="loading" @click="submit(hyFormRef)">确认</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import HyForm from '/@/components/HyForm/index.vue';
import { completeTask, runPathShowTop } from '/@/api/flow/task/index.ts';
import { changeApprove, terminateApprove, appImplementation } from '/@/api/Implementation/tableImple';
import { declareApprove } from '/@/api/decManagement/detail';
import TsyUploadFile from '/@/components/Upload/TsyIndex.vue';
import { useDict } from '/@/hooks/dict';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
const router = useRouter();
const route = useRoute();
// 定义字典
const { audit_operation_type } = useDict('audit_operation_type');
// 定义父组件传过来的值
const props = defineProps({
	flowData: {
		type: Object,
		default: () => {
			return {};
		},
	},
	processInstanceId: {
		type: String,
		default: () => {
			return '';
		},
	},
	title: {
		type: String,
		default: () => {
			return '';
		},
	},
});
const dialogVisible = ref(false);
const topData = ref({
	deptName: '',
	operationUser: '',
	approveResult: null,
	operationTime: '',
	approveDesc: '',
});
onMounted(() => {
	nextTick(() => {
		runPathShowTop({ processInstanceId: props.flowData.processInstanceId }).then((res: any) => {
			topData.value = res.data;
		});
	});
});
const loading = ref(false);
const submit = (formEl: any) => {
	if (!formEl) return;
	formEl.validate((valid: any, fields: any) => {
		if (valid) {
			// console.log('参数', hyFormRef.value.hyForm);
			// let parms = {
			// 	paramMap: {},
			// 	taskId: props.flowData.taskId,
			// 	taskLocalParamMap: {
			// 		approveDesc: hyFormRef.value.hyForm.approveDesc,
			// 		fileUrl: hyFormRef.value.hyForm.fileUrl,
			// 	},
			// };
			// parms.paramMap[props.flowData.nodeId + '_approve_condition'] = hyFormRef.value.hyForm.type == 'true' ? true : false;
			let parms = {
				processInstanceId: props.flowData.processInstanceId,
				taskId: props.flowData.taskId,
				nodeId: props.flowData.nodeId,
				appCode: props.flowData.appCode,
				pass: hyFormRef.value.hyForm.pass == 'true' ? true : false,
				advice: hyFormRef.value.hyForm.advice,
				files: hyFormRef.value.hyForm.files,
			};

			loading.value = true;
			if (props.title == 'impDecs') {
				appImplementation(parms)
					.then((res) => {
						loading.value = false;
						ElMessage({
							message: '操作成功',
							type: 'success',
						});
						dialogVisible.value = false;
						refreshCurrentTagsView();
					})
					.catch(() => {
						loading.value = false;
					});
			}
			if (props.title == 'appChangs') {
				changeApprove(parms)
					.then((res) => {
						loading.value = false;
						ElMessage({
							message: '操作成功',
							type: 'success',
						});
						dialogVisible.value = false;
						refreshCurrentTagsView();
					})
					.catch(() => {
						loading.value = false;
					});
			}
			if (props.title == 'implTers') {
				terminateApprove(parms)
					.then((res) => {
						loading.value = false;
						ElMessage({
							message: '操作成功',
							type: 'success',
						});
						dialogVisible.value = false;
						refreshCurrentTagsView();
					})
					.catch(() => {
						loading.value = false;
					});
			}
			if (props.title == 'numDecs') {
				declareApprove(parms)
					.then((res) => {
						loading.value = false;
						ElMessage({
							message: '操作成功',
							type: 'success',
						});
						dialogVisible.value = false;
						refreshCurrentTagsView();
					})
					.catch(() => {
						loading.value = false;
					});
			}
		}
	});
};
const hyFormRef: any = ref(null);
//关闭当前导航标签页面
const refreshCurrentTagsView = () => {
	window.history.length > 1 ? router.go(-1) : router.push('/');
};
//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		pass: {
			label: '审批结果',
			type: 'select',
			required: true,
			options: audit_operation_type.value,
		},
		advice: {
			label: '审批意见',
			type: 'textarea',
			required: true,
		},
		files: {
			label: '附件',
			type: 'upload',
			required: false,
		},
	};
});
//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '120px',
		span: 24,
		offset: 0,
		type: 'submit',
	};
});
// 暴露
defineExpose({ dialogVisible });
</script>
<style scoped lang="scss">
.flow-btn {
	position: absolute;
	bottom: 0px;
	left: 15px;
	z-index: 66;
	width: calc(100% - 35px);
	.flow-card {
		display: flex;
		justify-content: flex-end;
	}
}
:deep(.my-1 span:first-child) {
	display: none;
}
</style>
